package com.xiyu.service.controller.admin.mailbox;
import com.xiyu.service.vo.mailbox.mail.MailGetReceiverByAddressesOutput;
import com.xiyu.service.vo.mailbox.mail.MailGetReceiverByAddressesInput;
import com.xiyu.service.vo.mailbox.mail.MailSaveMailAsFileOutput;
import com.xiyu.service.vo.mailbox.mail.MailSaveMailInput;
import com.xiyu.service.vo.mailbox.mail.MailReceiverMultiQueryOutput;
import com.xiyu.service.vo.mailbox.mail.MailReceiverMultiQueryInput;
import com.xiyu.service.vo.mailbox.mail.MailClearMailLabelInput;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.vo.mailbox.mail.MailGetMailDetailOutput;
import com.xiyu.service.vo.mailbox.mail.MailSetMailJunkInput;
import com.xiyu.service.vo.mailbox.mail.MailMoveFolderOutput;
import com.xiyu.service.vo.mailbox.mail.MailMoveFolderInput;
import com.xiyu.service.vo.mailbox.mail.MailSetMailLabelInput;
import com.xiyu.service.vo.mailbox.mail.MailSetMailTopInput;
import com.xiyu.service.vo.mailbox.mail.MailSetMailReadInput;
import com.xiyu.service.vo.mailbox.mail.MailPageMailOutput;
import com.xiyu.service.vo.mailbox.mail.MailPageMailInput;
import com.xiyu.service.vo.mailbox.mail.MailLabelSortInput;
import com.xiyu.service.vo.mailbox.mail.MailSingleGetLabelOutput;
import com.xiyu.service.vo.mailbox.mail.MailUpdateLabelInput;
import com.xiyu.service.vo.mailbox.mail.MailCreateLabelInput;
import com.xiyu.service.vo.mailbox.mail.MailListLabelOutput;
import com.xiyu.service.vo.mailbox.mail.MailUpdateFolderInput;
import com.xiyu.service.vo.mailbox.mail.MailCreateFolderInput;
import com.xiyu.service.vo.mailbox.mail.MailGetFolderByAccountIdsOutput;
import com.xiyu.service.framework.web.web.core.pojo.CommonResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import static com.xiyu.service.framework.web.web.core.pojo.CommonResult.success;

import com.xiyu.service.service.mailbox.MailService;

import java.util.List;

@Tag(name = "邮件管理")
@RestController
@RequestMapping("/mailbox/mail")
@Validated
public class MailController {

    @Resource
    private MailService mailService;

    @GetMapping("/get-folder-by-account-ids")
    @Operation(summary = "根据账号获取文件夹信息")
    @Parameter(name = "accountIds", description = "邮箱账号", example = "")
    public CommonResult<List<MailGetFolderByAccountIdsOutput>> getFolderByAccountIds(@RequestParam("accountIds") List<Long> accountIds) {
        return success(mailService.getFolderByAccountIds(accountIds));
    }

    @PostMapping("/create-folder")
    @Operation(summary = "新增邮件文件夹")
    public CommonResult<Long> createFolder(@Valid @RequestBody MailCreateFolderInput inputVO) {
        return success(mailService.createFolder(inputVO));
    }

    @PutMapping("/update-folder")
    @Operation(summary = "更新邮箱文件夹")
    public CommonResult<Boolean> updateFolder(@Valid @RequestBody MailUpdateFolderInput inputVO) {
        return success(mailService.updateFolder(inputVO));
    }

    @DeleteMapping("/delete-folder")
    @Operation(summary = "删除文件夹")
    @Parameter(name = "id", description = "邮箱账号文件夹主键ID", example = "")
    public CommonResult<Boolean> deleteFolder(@RequestParam("id") Long id) {
        return success(mailService.deleteFolder(id));
    }

    @GetMapping("/list-label")
    @Operation(summary = "查询邮件标签")
    @Parameter(name = "ownerId", description = "所属人ID", example = "")
    public CommonResult<List<MailListLabelOutput>> listLabel(@RequestParam("ownerId") Long ownerId) {
        return success(mailService.listLabel(ownerId));
    }

    @PostMapping("/create-label")
    @Operation(summary = "新增邮件标签")
    public CommonResult<Long> createLabel(@Valid @RequestBody MailCreateLabelInput inputVO) {
        return success(mailService.createLabel(inputVO));
    }

    @PutMapping("/update-label")
    @Operation(summary = "更新邮件标签")
    public CommonResult<Boolean> updateLabel(@Valid @RequestBody MailUpdateLabelInput inputVO) {
        return success(mailService.updateLabel(inputVO));
    }

    @GetMapping("/single-get-label")
    @Operation(summary = "获取单个标签")
    @Parameter(name = "id", description = "邮件标签主键ID", example = "")
    public CommonResult<MailSingleGetLabelOutput> singleGetLabel(@RequestParam("id") Long id) {
        return success(mailService.singleGetLabel(id));
    }

    @PostMapping("/label-sort")
    @Operation(summary = "标签排序")
    public CommonResult<Boolean> labelSort(@Valid @RequestBody MailLabelSortInput inputVO) {
        return success(mailService.labelSort(inputVO));
    }

    @DeleteMapping("/delete-label")
    @Operation(summary = "删除标签")
    @Parameter(name = "id", description = "邮箱账号文件夹主键ID", example = "")
    public CommonResult<Boolean> deleteLabel(@RequestParam("id") Long id) {
        return success(mailService.deleteLabel(id));
    }

    @GetMapping("/get-unread-top-mail")
    @Operation(summary = "获取未读的置顶邮件数量")
    @Parameter(name = "accountIds", description = "邮箱账号主键ID", example = "")
    public CommonResult<Long> getUnreadTopMail(@RequestParam("accountIds") List<Long> accountIds) {
        return success(mailService.getUnreadTopMail(accountIds));
    }

    @GetMapping("/page-mail")
    @Operation(summary = "根据文件夹ID获取邮件信息")
    public CommonResult<PageResult<MailPageMailOutput>> pageMail(@Valid MailPageMailInput inputVO) {
        return success(mailService.pageMail(inputVO));
    }

    @PostMapping("/set-mail-read")
    @Operation(summary = "设置邮件是否已读")
    public CommonResult<Boolean> setMailRead(@Valid @RequestBody MailSetMailReadInput inputVO) {
        return success(mailService.setMailRead(inputVO));
    }

    @PostMapping("/set-mail-top")
    @Operation(summary = "设置邮件是否置顶")
    public CommonResult<Boolean> setMailTop(@Valid @RequestBody MailSetMailTopInput inputVO) {
        return success(mailService.setMailTop(inputVO));
    }

    @PostMapping("/set-mail-label")
    @Operation(summary = "设置邮件标签")
    public CommonResult<Boolean> setMailLabel(@Valid @RequestBody MailSetMailLabelInput inputVO) {
        return success(mailService.setMailLabel(inputVO));
    }

    @PostMapping("/move-folder")
    @Operation(summary = "邮件移动文件夹")
    public CommonResult<List<MailMoveFolderOutput>> moveFolder(@Valid @RequestBody MailMoveFolderInput inputVO) {
        return success(mailService.moveFolder(inputVO));
    }

    @PostMapping("/set-mail-junk")
    @Operation(summary = "设置邮件是否是垃圾邮件")
    public CommonResult<Boolean> setMailJunk(@Valid @RequestBody MailSetMailJunkInput inputVO) {
        return success(mailService.setMailJunk(inputVO));
    }

    @DeleteMapping("/delete-mail")
    @Operation(summary = "删除邮件")
    @Parameter(name = "mailIds", description = "邮件主键ID", example = "")
    public CommonResult<Boolean> deleteMail(@RequestParam("mailIds") List<Long> mailIds) {
        return success(mailService.deleteMail(mailIds));
    }

    @GetMapping("/get-mail-detail")
    @Operation(summary = "获取邮件详情")
    @Parameter(name = "id", description = "邮件主键ID", example = "")
    public CommonResult<MailGetMailDetailOutput> getMailDetail(@RequestParam("id") Long id) {
        return success(mailService.getMailDetail(id));
    }

    @PostMapping("/set-folder-all-mail-read")
    @Operation(summary = "设置文件夹下全部邮件为已读")
    @Parameter(name = "id", description = "邮箱账号文件夹主键ID", example = "")
    public CommonResult<Boolean> setFolderAllMailRead(@RequestParam("id") Long id) {
        return success(mailService.setFolderAllMailRead(id));
    }

    @PostMapping("/clear-mail-label")
    @Operation(summary = "清除邮件标签")
    public CommonResult<Boolean> clearMailLabel(@Valid @RequestBody MailClearMailLabelInput inputVO) {
        return success(mailService.clearMailLabel(inputVO));
    }

    @PostMapping("/receiver-multi-query")
    @Operation(summary = "查找收件人")
    public CommonResult<List<MailReceiverMultiQueryOutput>> receiverMultiQuery(@Valid @RequestBody MailReceiverMultiQueryInput inputVO) {
        return success(mailService.receiverMultiQuery(inputVO));
    }

    @PostMapping("/save-mail")
    @Operation(summary = "保存或发送邮件")
    public CommonResult<Long> saveMail(@Valid @RequestBody MailSaveMailInput inputVO) {
        return success(mailService.saveMail(inputVO));
    }

    @PostMapping("/save-mail-as-file")
    @Operation(summary = "将邮件保存成文件")
    @Parameter(name = "id", description = "邮件主键ID", example = "")
    public CommonResult<MailSaveMailAsFileOutput> saveMailAsFile(@RequestParam("id") Long id) {
        return success(mailService.saveMailAsFile(id));
    }

    @PostMapping("/get-receiver-by-addresses")
    @Operation(summary = "根据邮件获取客户联系人信息")
    public CommonResult<List<MailGetReceiverByAddressesOutput>> getReceiverByAddresses(@Valid @RequestBody List<MailGetReceiverByAddressesInput> inputVO) {
        return success(mailService.getReceiverByAddresses(inputVO));
    }

}
